﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MqttRedis
{
    public partial class Redis : Form
    {
        IDatabase Db { get; set; }
        ConnectionMultiplexer Connection { get; set; }
        List<Dictionary<string, object>> List = new();
        List<string> Checks = new();
        public Redis()
        {
            InitializeComponent();
            TBRedisConnect.Text = "39.105.189.49:6379,password=Sy@12349,allowadmin=true";
            //TBRedisConnect.Text = "127.0.0.1:6379,password=Sy@12349,allowadmin=true";
        }

        private void BTRedisConnect_Click(object sender, EventArgs e)
        {
            Connection = ConnectionMultiplexer.Connect(TBRedisConnect.Text);
            Db = Connection.GetDatabase(6);
            Init();
        }

        private void BTRedisDDisconnect_Click(object sender, EventArgs e)
        {
            if (Connection.IsConnected)
            {
                Connection.Close();
                RedisTree.Nodes.Clear();
                TBContent.Text = "";
            }
        }

        private void Init()
        {
            string ip = "39.105.189.49:6379";
            //string ip = "127.0.0.1:6379";
            TreeNode root = new(ip);
            IServer server = Connection.GetServer(ip);
            for (int i = 0; i < server.DatabaseCount; i++)
            {
                IEnumerable<RedisKey> keys = server.Keys(i);
                if (keys.Any())
                {
                    TreeNode one = root.Nodes.Add("database" + i); foreach (string item in keys)
                    {
                        one.Nodes.Add(item);
                    }
                }
            }

            RedisTree.Nodes.Clear();
            RedisTree.Nodes.Add(root);
            RedisTree.ExpandAll();
        }

        private void RedisTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 2)
            {
                string key = e.Node.Text;
                string parent = e.Node.Parent.Text.Replace("database", "");
                int database = Convert.ToInt32(parent);

                Db = Connection.GetDatabase(database);
                string data = Db.StringGet(key);

                List = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(data);

                CLBProperty.Items.Clear();
                foreach (KeyValuePair<string, object> item in List[0])
                {
                    CLBProperty.Items.Add(item.Key);
                }

                TBContent.Text = data;
            }
            else
            {
                MessageBox.Show("当前选中节点不能查询key数据");
            }
        }

        private void BTRedisDelete_Click(object sender, EventArgs e)
        {
            string key = RedisTree.SelectedNode.Text;
            Db.KeyDelete(key);
            Init();
        }

        private void BTNChecked_Click(object sender, EventArgs e)
        {
            if (CLBProperty.Items.Count == 0)
            {
                MessageBox.Show("请选择要查询的字段");
            }
            else
            {
                string content = "";
                foreach (Dictionary<string, object> list in List)
                {
                    string temp = "{";
                    foreach (KeyValuePair<string, object> item in list)
                    {
                        if (Checks.Contains(item.Key))
                        {
                            temp += $"\"{item.Key}\":\"{item.Value}\",";
                        }
                    }
                    temp = temp.TrimEnd(',') + "}\r\n";
                    content += temp;
                }
                TBContent.Text = content;
            }
        }

        private void CLBProperty_SelectedValueChanged(object sender, EventArgs e)
        {
            string key = CLBProperty.SelectedItem.ToString();
            int index = CLBProperty.SelectedIndex;
            bool chenked = CLBProperty.GetItemChecked(index);
            if (chenked)
            {
                Checks.Add(key);
            }
            else
            {
                Checks.Remove(key);
            }
        }
    }

    /// <summary>
    /// 设备告警数据
    /// </summary>
    public class WT_Alarm
    {
        /// <summary>
        /// 主键
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 厕所主键
        /// </summary>
        public string ToiletId { get; set; }

        /// <summary>
        /// 坑位主键
        /// </summary>
        public string PitId { get; set; }

        /// <summary>
        /// 告警类型
        /// </summary>
        public int AlarmType { get; set; }

        /// <summary>
        /// 告警等级
        /// </summary>
        public int AlarmLevel { get; set; }

        /// <summary>
        /// 实际值
        /// </summary>
        public decimal ActualValue { get; set; }

        /// <summary>
        /// 预警值
        /// </summary>
        public decimal AlarmValue { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 告警状态
        /// </summary>
        public int AlarmState { get; set; }

        /// <summary>
        /// 告警地点
        /// </summary>
        public int AlarmLocation { get; set; }

        /// <summary>
        /// 告警名称
        /// </summary>
        public string AlarmName { get; set; }

        /// <summary>
        /// 告警原因
        /// </summary>
        public string AlarmReason { get; set; }

        /// <summary>
        /// 告警结果
        /// </summary>
        public string AlarmResult { get; set; }

        /// <summary>
        /// 告警内容
        /// </summary>
        public string AlarmContent { get; set; }

        /// <summary>
        /// 通知部门
        /// </summary>
        public string InformDepartment { get; set; }

        /// <summary>
        /// 通知方式
        /// </summary>
        public string InformWay { get; set; }

        /// <summary>
        /// 通知内容（即告警内容）
        /// </summary>
        public string InformContent { get; set; }

        /// <summary>
        /// 处理方式
        /// </summary>
        public int HandleWay { get; set; }

        /// <summary>
        /// 处理结果
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 操作人
        /// </summary>
        public string Operator { get; set; }

        /// <summary>
        /// 部门主键
        /// </summary>
        public int DepartmentId { get; set; }
    }
}
